package com.yoomiito.app.ui.fragment.my;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
//import com.netease.demo.live.activity.EnterLiveActivity;
import com.yoomiito.app.Constant;
import com.yoomiito.app.ImmersionBarFragment;
import com.yoomiito.app.R;
import com.yoomiito.app.adapter.my.MyBillAdapter;
import com.yoomiito.app.adapter.my.MyFunctionAdapter;
import com.yoomiito.app.address.AddressListActivity;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.WebViewActivity;
import com.yoomiito.app.model.CreditCard;
import com.yoomiito.app.model.bean.UserInfo;
import com.yoomiito.app.model.bean.WalletUrl;
import com.yoomiito.app.model.my.BillItemBean;
import com.yoomiito.app.model.my.FunctionBean;
import com.yoomiito.app.model.my.MyUserCenterInfo;
import com.yoomiito.app.model.oilcard.OilCardData;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.ui.college.list.CollegeSubjectActivity;
import com.yoomiito.app.ui.gift.GiftActivity;
import com.yoomiito.app.ui.goods.NewGoodsInfoActivity;
import com.yoomiito.app.ui.my.BalanceActivity;
import com.yoomiito.app.ui.my.FindActivity;
import com.yoomiito.app.ui.my.MyIncomeActivity;
import com.yoomiito.app.ui.my.MySaleActivity;
import com.yoomiito.app.ui.my.RealNameActivity;
import com.yoomiito.app.ui.my.income.IncomeForecastActivity;
import com.yoomiito.app.ui.my.instrument.InstrumentActivity;
import com.yoomiito.app.ui.my.maintain.MaintainActivity;
import com.yoomiito.app.ui.my.pick.PickGoodsActivity;
import com.yoomiito.app.ui.my.remain.PendingOrderActivity;
import com.yoomiito.app.ui.my.remain.deduct.DeductMoneyActivity;
import com.yoomiito.app.ui.my.remain.withdraw.WithdrawListActivity;
import com.yoomiito.app.ui.my.repurchase.RepurchaseActivity;
import com.yoomiito.app.ui.my.songdudouo.GiveMoreIncomeActivity;
import com.yoomiito.app.ui.my.stock.StockActivity;
import com.yoomiito.app.ui.order.list.OrderActivity;
import com.yoomiito.app.present.my.fragment.MyFmPresenter;
import com.yoomiito.app.share.ui.NewInviteShareActivity;
import com.yoomiito.app.ui.list.GoodsSortListActivity;
import com.yoomiito.app.ui.my.MyCollectGoodsActivity;
import com.yoomiito.app.ui.my.MyHistoryIncomeActivity;
import com.yoomiito.app.ui.my.MyInviteActivity;
import com.yoomiito.app.ui.my.MyPublishMaterialActivity;
import com.yoomiito.app.ui.my.angle.AngleActivity;
import com.yoomiito.app.ui.my.remain.MyRemainActivity;
import com.yoomiito.app.ui.my.MyTodayOrderActivity;
import com.yoomiito.app.ui.my.MyUserInfoActivity;
import com.yoomiito.app.ui.my.MyVipOrderActivity;
import com.yoomiito.app.ui.my.SetActivity;
import com.yoomiito.app.ui.my.teamallowance.TeamAllowanceOrderActivity;
import com.yoomiito.app.ui.songduoduo.SendMoreActivity;
import com.yoomiito.app.ui.yijiayou.OilCardDetailActivity;
import com.yoomiito.app.ui.yijiayou.OilCardView;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.Const;
import com.yoomiito.app.utils.EventMessage;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.Type;
import com.yoomiito.app.utils.UserInfoConfig;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.utils.wx.WXUtils;
import com.yoomiito.app.widget.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.router.Router;

import static com.yoomiito.app.ExtensionFunKt.showToast;
import static com.yoomiito.app.ui.my.remain.PendingOrderActivity.KEY_ACTIVITY_TYPE;
import static com.yoomiito.app.ui.yijiayou.OilCardView.OilCardGoodsId;


/**
 * 我的
 */
public class MyFragment extends ImmersionBarFragment<MyFmPresenter> {

    @BindView(R.id.rv_function)
    RecyclerView mFunctionRV;

    @BindView(R.id.rv_user_function)
    RecyclerView mUserFunctionRV;

    @BindView(R.id.rv_bill)
    RecyclerView mBillRV;

    @BindView(R.id.iv_mine)
    CircleImageView mPhotoIv;
    @BindView(R.id.tv_user_name)
    TextView userNameTv;
    @BindView(R.id.angle)
    ImageView mAngleIv;
    @BindView(R.id.tv_invitation_code)
    TextView invitationCodeTv;

    @BindView(R.id.tv_num)
    TextView invitationNum;
    @BindView(R.id.tv_amount)
    TextView amountTv;

    @BindView(R.id.tv_total_amount)
    TextView totalAmountTv;

    @BindView(R.id.show_youmi)
    TextView mShowYouMi;

    @BindView(R.id.tv_copy)
    TextView copyTv;

//    @BindView(R.id.iv_become_vip)
//    ImageView becomeVipTv;

    @BindView(R.id.layout_top)
    LinearLayout topLayout;

    @BindView(R.id.ll_invite)
    LinearLayout inviteLayout;

    @BindView(R.id.iv_vip)
    TextView vipTv;
    @BindView(R.id.head_king)
    ImageView quDaiIv;
    @BindView(R.id.top_relative)
    RelativeLayout mRelativeLayout;
    @BindView(R.id.youmiDetail)
    LinearLayout youmiDetail;
    @BindView(R.id.youmi)
    TextView youmi;

    //级别 0--会员  4--粉丝 10--vip 20 30 40 50---运营中心
    private MyUserCenterInfo myUserCenterInfo;
    private MyFunctionAdapter mFunctionAdapter, mUserFunctionAdapter;
    private MyBillAdapter mBillAdapter;
    private static final String StarStore = "星店商城";
    private MyUserCenterInfo mInfo;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_my;
    }

    private List<FunctionBean> mFunctionList, mUserFunctionList;
    private List<BillItemBean> billItemBeanList;

    @Override
    public MyFmPresenter newP() {
        return new MyFmPresenter(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            myUserCenterInfo = savedInstanceState.getParcelable(Constant.UserInfo);
        }
        totalAmountTv.setTypeface(AllUtils.getTypeface());
        amountTv.setTypeface(AllUtils.getTypeface());
        mShowYouMi.setTypeface(AllUtils.getTypeface());
        mRelativeLayout.post(() -> {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mRelativeLayout.getLayoutParams();
            lp.width = lp.height;
            mRelativeLayout.setLayoutParams(lp);
        });

        initFunctionData();
    }

    private boolean isSpecialAccount() {
        return !"18124576245".equals(UserInfoConfig.getUser().getMobile());
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        outState.putParcelable(Constant.UserInfo, myUserCenterInfo);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onStartLazy() {
        super.onStartLazy();
        if (App.mAppConfig.getIsLogin()) {
            getP().getUserCenterData();
            getP().getUserInfo();
            getOilCardInfo();
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    private void initFData() {
        Log.e("xing", "initFData mInfo = " + mInfo);
        if (mUserFunctionList == null) {
            mUserFunctionList = new ArrayList<>();
        } else {
            mUserFunctionList.clear();
        }
        youmiDetail.setVisibility((mInfo != null && "1".equals(mInfo.getOilCard())) ? View.VISIBLE : View.GONE);
        youmiDetail.setOnClickListener(v -> {
            OilCardDetailActivity.Companion.start(context);
        });
//        mUserFunctionList.add(new FunctionBean(ResUtil.getString(R.string.my_order), R.mipmap.my_order, false));
//        mUserFunctionList.add(new FunctionBean(ResUtil.getString(R.string.my_publish), R.mipmap.my_release,false));
//        mUserFunctionList.add(new FunctionBean(ResUtil.getString(R.string.my_collect_goods), R.drawable.icon_love, false));
//        mUserFunctionList.add(new FunctionBean(ResUtil.getString(R.string.my_coupon), R.mipmap.my_coupon));

        mUserFunctionList.add(new FunctionBean("我要提货", R.mipmap.my_delivery, false));

        mUserFunctionList.add(new FunctionBean("我的用户", R.mipmap.icon_wodeyonghu, false));

        mUserFunctionList.add(new FunctionBean("五不高复购", R.drawable.icon_fugou, false));
        mUserFunctionList.add(new FunctionBean("套盒复购", R.mipmap.icon_mine1, false));

        if (mInfo != null && mInfo.param2 == 0) {
            mUserFunctionList.add(new FunctionBean("仪器耗材", R.mipmap.icon_mine2, false));
        }

        mUserFunctionList.add(new FunctionBean("提现信息", R.drawable.icon_txxx, false));

        mUserFunctionList.add(new FunctionBean("实名认证", R.drawable.icon_real_name, false));
        if (mInfo != null && mInfo.roleId != 0) {
            mUserFunctionList.add(new FunctionBean("我的业绩", R.drawable.icon_yeji, false));
        }
        if (mInfo != null && "1".equals(mInfo.getBusinessCollege())) {
            mUserFunctionList.add(new FunctionBean(ResUtil.getString(R.string.my_college), R.mipmap.my_colloge, false));
        }
        if (mInfo != null && "1".equals(mInfo.getAngelPlan())) {
            mUserFunctionList.add(new FunctionBean("天使计划", R.drawable.icon_angle_1, false));
        }
        if (mInfo != null && "1".equals(mInfo.getSendMore())) {
            mUserFunctionList.add(new FunctionBean("送多多", R.mipmap.gift_active, false));
        }
        if (UserInfoConfig.IsAgent()) {
            mUserFunctionList.add(new FunctionBean("区代商城", R.drawable.icon_qudai, false));
        }
//        mUserFunctionList.add(new FunctionBean(ResUtil.getString(R.string.my_publish_material), R.mipmap.my_material, false));
        if (mInfo != null && "1".equals(mInfo.getStarStore()))
            if (UserInfoConfig.getCompanyId() == 3) {
                mUserFunctionList.add(new FunctionBean(StarStore, R.drawable.icon_star, false));
            }
//        if (isSpecialAccount()) {
        if (mInfo != null && "1".equals(mInfo.getYmFinance())) {
            mUserFunctionList.add(new FunctionBean("智能用卡", R.drawable.my_finance, false));
        }
        if (mInfo != null && "1".equals(mInfo.getYmLive())) {
            mUserFunctionList.add(new FunctionBean("千年艾直播", R.drawable.icon_zhibo, false));
        }
        if (mInfo != null && "1".equals(mInfo.getOilCard())) {
            mUserFunctionList.add(new FunctionBean("立即充值", R.drawable.youzhan, false));
        }
//        }

        if (UserInfoConfig.getRoleId() >= Type.LevelType.VIP_20) {
        }
        if (mUserFunctionAdapter != null) mUserFunctionAdapter.notifyDataSetChanged();


    }

    private void initFunctionData() {
        initFData();
//        if (App.mLiveInfo != null && App.mLiveInfo.getIs_anchor() == 1) {
//            mUserFunctionList.add(new FunctionBean(ResUtil.getString(R.string.my_open_live), R.mipmap.my_live,false));
//        }
//        mUserFunctionList.add(new FunctionBean(ResUtil.getString(R.string.my_playback_live), R.mipmap.my_live_play_back));

        GridLayoutManager gridLayoutManagerUser = new GridLayoutManager(getActivity(), 3);
        mUserFunctionRV.setLayoutManager(gridLayoutManagerUser);
        mUserFunctionRV.setNestedScrollingEnabled(false);
        mUserFunctionAdapter = new MyFunctionAdapter(mUserFunctionList);
        mUserFunctionRV.setAdapter(mUserFunctionAdapter);
        mUserFunctionAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (AllUtils.isFastClick()) {
                    return;
                }
                FunctionBean functionBean = mUserFunctionAdapter.getData().get(position);
                if (App.mAppConfig.getIsLogin()) {

                    Intent intent = null;
                    switch (functionBean.getTitle()) {
                        case "我的订单":
                            intent = new Intent(context, OrderActivity.class);
                            break;
                        case "我的发布":
                            intent = new Intent(context, MyCircleActivity.class);
                            break;
                        case "商品收藏":
                            intent = new Intent(context, MyCollectGoodsActivity.class);
                            break;
                        case "我要提货":
                            Router.newIntent(context)
                                    .to(PickGoodsActivity.class)
                                    .launch();
                            break;
                        case "我的用户":
                            intent = new Intent(context, MyInviteActivity.class);
//                            intent.putExtra(MyInviteActivity.KEY_INVITE_NUM, Integer.parseInt(invitationNum.getText().toString()));
                            break;
                        case "五不高复购":
                            intent = new Intent(context, MaintainActivity.class);
                            break;
                        case "套盒复购":
                            intent = new Intent(context, RepurchaseActivity.class);
                            break;
                        case "仪器耗材":
                            intent = new Intent(context, InstrumentActivity.class);
                            break;
                        case "提现信息":
                            WithdrawListActivity.Companion.start(context,1);
                            break;
                        case "实名认证":
                            intent = new Intent(context, RealNameActivity.class);
                            break;
                        case "我的业绩":
                            intent = new Intent(context, MySaleActivity.class);
                            break;
                        case "区代商城"://区代商城
                            GoodsSortListActivity.Companion.start(context, "区代商城", AllCode.AgentMall);
                            return;
                        case "宣传素材":
                            intent = new Intent(context, MyPublishMaterialActivity.class);
                            intent.putExtra(Constant.TYPE, Constant.COMMUNITY_TYPE_INVITE_MATERIAL);
                            break;
                        case "商学院":
//                            intent = new Intent(context, MyPublishMaterialActivity.class);
//                            intent.putExtra(Constant.TYPE, Constant.COMMUNITY_TYPE_TRADE_COLLEAGE);
                            CollegeSubjectActivity.Companion.start(context);
                            break;
                        case StarStore:
                            GoodsSortListActivity.Companion.start(context, StarStore, AllCode.StarStore);
                            break;
//                        case 7:
//                            EnterLiveActivity.start(getContext(),
//                                    App.mLiveInfo.getLive_info().getPush_url(),//推流地址
//                                    String.valueOf(App.mLiveInfo.getLive_info().getVip_product_id()),//商品id
//                                    App.mLiveInfo.getLive_info().getProduct_name(),
//                                    String.valueOf(App.mLiveInfo.getLive_info().getId()),
//                                    App.mLiveInfo.getLive_info().getCid());//商品名称
//                            break;
                        case "天使计划":
                            AngleActivity.Companion.start(context);
                            break;
                        case "送多多":
                            SendMoreActivity.start(context);
                            break;
                        case "智能用卡":
                            showLoading();
                            getP().creditCardUrl();
                            break;
//                        case "优米圈":
//                            FindActivity.start(context);
//                            break;
                        case "千年艾直播":
                            WXUtils.INSTANCE.openMiniProgram(context, "/pages/lives/index");
                            break;
                        case "立即充值":
                            NewGoodsInfoActivity.Companion.toGoodsInfo(context, OilCardGoodsId, Type.ServiceConfigGoodsType.GoodsMainTypePrivateGoods + "");
                            break;
                    }
                    if (intent != null) {
                        startActivity(intent);
                    }
                }
            }
        });

        mFunctionList = new ArrayList<>();
        mFunctionList.add(new FunctionBean("邀请好友", R.drawable.icon_my_invitation, false));
        mFunctionList.add(new FunctionBean(ResUtil.getString(R.string.my_receive_addr), R.mipmap.my_adress, false));
//        mFunctionList.add(new FunctionBean(ResUtil.getString(R.string.my_find_order), R.mipmap.my_search));
        mFunctionList.add(new FunctionBean("东方元直播", R.drawable.icon_zhibo, false));

//        mFunctionList.add(new FunctionBean(ResUtil.getString(R.string.my_faq), R.mipmap.my_problem, false));
        mFunctionList.add(new FunctionBean(ResUtil.getString(R.string.my_service), R.mipmap.my_service, false));
        mFunctionList.add(new FunctionBean(ResUtil.getString(R.string.my_setting), R.mipmap.my_set, false));

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
        mFunctionRV.setLayoutManager(gridLayoutManager);
        mFunctionRV.setNestedScrollingEnabled(false);
        mFunctionAdapter = new MyFunctionAdapter(mFunctionList);
        mFunctionRV.setAdapter(mFunctionAdapter);
        mFunctionAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (AllUtils.isFastClick()) {
                return;
            }
            if (App.mAppConfig.getIsLogin()) {
                String title = mFunctionList.get(position).getTitle();
                switch (title) {
                    case "我要提货":
                        Router.newIntent(context)
                                .to(PickGoodsActivity.class)
                                .launch();
                        break;
                    case "邀请好友":
                        String inviteCode = invitationCodeTv.getText().toString();
                        if (SpannableUtils.isEmptyOr0(inviteCode)) {
                            YMToast.showText("请检查邀请码是否存在");
                            return;
                        }
                        Intent intent = new Intent(context, NewInviteShareActivity.class);
                        intent.putExtra(Constant.TYPE, AllCode.InviteFriend);
                        startActivity(intent);
                        break;
                    case "收货地址":
                        AddressListActivity.Companion.start(context);
                        break;

                    case "东方元直播":
                        WXUtils.INSTANCE.openMiniProgram(context, "/pages/lives/index");
//                        Router.newIntent(context)
//                                .to(WebViewActivity.class)
//                                .putInt(Constant.TYPE, AllCode.NewVideo)
//                                .launch();
                        break;
                    case "常见问题":
                        Router.newIntent(context)
                                .to(WebViewActivity.class)
                                .putInt(Constant.TYPE, AllCode.FAQ)
                                .launch();
                        break;
                    case "专属客服":
                        int type = AllCode.CustomService;
//                        if (myUserCenterInfo != null && myUserCenterInfo.getUserInfo() != null && myUserCenterInfo.getUserInfo().getIsVip() == 1) {
//                            type = AllCode.CustomServiceVIP;
//                        }
                        Router.newIntent(context)
                                .to(WebViewActivity.class)
                                .putInt(Constant.TYPE, type)
                                .launch();
                        break;
                    case "设置":
                        Router.newIntent(context)
                                .to(SetActivity.class)
                                .launch();
                        break;
                }
            }

        });

        setVipLayout();
    }

    private void setVipLayout() {

        if (mBillRV == null) return;
//        if (UserInfoConfig.getRoleId() > LevelType.VIP_0) { // 在获取数据

        if (billItemBeanList == null) {
            billItemBeanList = new ArrayList<>();
        } else {
            billItemBeanList.clear();
        }

        if (mBillRV.getLayoutManager() == null) {
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
//        mBillRV.addItemDecoration(new MyItemDecoration(getActivity(), RecyclerView.VERTICAL));
            mBillRV.setLayoutManager(linearLayoutManager);
        }

        if (mBillAdapter == null) {
            mBillAdapter = new MyBillAdapter(billItemBeanList);
        }
        mBillRV.setNestedScrollingEnabled(false);
        mBillRV.setAdapter(mBillAdapter);
        mBillAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (AllUtils.isFastClick()) {
                return;
            }
            if (App.mAppConfig.getIsLogin()) {

                Intent intent = null;
                String key = billItemBeanList.get(position).getKey();
                if (key.contains("今日订单")) {
                    intent = new Intent(context, MyTodayOrderActivity.class);
                    intent.putExtra(Constant.KEY_ORDER_MONEY, myUserCenterInfo.getTodayIncome());
                } else if (key.contains("送多多收益")) {
                    GiveMoreIncomeActivity.Companion.start(context);
                } else if (key.contains("礼包/爆品/优品/课程")) {
                    intent = new Intent(context, MyVipOrderActivity.class);
                } else if (key.contains("管理津贴预计")) {
                    TeamAllowanceOrderActivity.Companion.start(context);
                } else if (key.contains("淘宝结算预计")) {
//                    intent = new Intent(context, MyMonthOrderActivity.class);
                    IncomeForecastActivity.Companion.start(context, IncomeForecastActivity.TAOBAO);
                } else if (key.contains("云库存明细")) {
                    intent = new Intent(context, StockActivity.class);
                    intent.putExtra(KEY_ACTIVITY_TYPE, 1);
                } else if (key.contains("实体库存明细")) {
                    intent = new Intent(context, StockActivity.class);
                    intent.putExtra(KEY_ACTIVITY_TYPE, 2);
                } else if (key.contains("京东结算预计")) {
                    IncomeForecastActivity.Companion.start(context, IncomeForecastActivity.JINGDONG);
//                    intent = new Intent(context, MyMonthOrderActivity.class);
                    /* 前需求：分别显示两个月的结算预计
                    if (position == billItemBeanList.size() - 1) {
                        intent = new Intent(context, MyMonthOrderActivity.class);
                        Bundle bundle2 = new Bundle();
                        bundle2.putStringArrayList(Constant.KEY_MONTH_DATA, billItemBeanList.get(billItemBeanList.size() - 1).getData());
                        intent.putExtras(bundle2);
                    } else {
                        intent = new Intent(context, MyMonthOrderActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putStringArrayList(Constant.KEY_MONTH_DATA, billItemBeanList.get(billItemBeanList.size() - 2).getData());
                        intent.putExtras(bundle);
                    }
                     */
                } else if (key.contains("智能用卡结算预计")) {
                    IncomeForecastActivity.Companion.start(context, IncomeForecastActivity.CreditCard);
                }
                if (intent != null) {
                    intent.putExtra(Constant.KEY_ORDER_MONEY, billItemBeanList.get(position).getValue());
                    startActivity(intent);
                }
            }
        });
//        }
    }

    public void setCreditCard(CreditCard creditCard) {
        dismissLoading();
        if (!TextUtils.isEmpty(creditCard.getUrl())) {
            WebViewActivity.start(context, creditCard.getUrl(), Const.Url.CreditCard);
        }
    }

    @OnClick({R.id.iv_mine, R.id.ll_money, R.id.ll_my_order, R.id.ll_kehu, R.id.ll_pending, R.id.youmi_ll, R.id.ll_invite, R.id.tv_copy, R.id.layout_amout})
    public void onClick(View view) {
        if (AllUtils.isFastClick()) {
            return;
        }
        if (App.mAppConfig.getIsLogin()) {

            Intent intent = null;
            switch (view.getId()) {
                case R.id.iv_mine:
                    intent = new Intent(context, MyUserInfoActivity.class);
                    context.startActivity(intent);
                    break;
                case R.id.layout_amout:
                    intent = new Intent(context, MyRemainActivity.class);
                    intent.putExtra(MyRemainActivity.REMAIN_MONEY_KEY, amountTv.getText().toString());
                    intent.putExtra(MyRemainActivity.KEY_ACTIVITY_TYPE, 1);
                    startActivity(intent);
                    break;
                case R.id.youmi_ll:
                    intent = new Intent(context, MyRemainActivity.class);
                    intent.putExtra(MyRemainActivity.REMAIN_MONEY_KEY, mShowYouMi.getText().toString());
                    intent.putExtra(MyRemainActivity.KEY_ACTIVITY_TYPE, 2);
                    startActivity(intent);
                    break;
                case R.id.ll_money:
                    if (mInfo.roleId != 0)
                        gotoActivity(MyIncomeActivity.class);
                    break;
                case R.id.ll_invite:

                    if (mInfo.realName == 1) {
                        getP().getWalletUrl();
                    } else {
                        showToast("您需要先实名哦!");
                    }


//                    intent = new Intent(context, MyInviteActivity.class);
//                    intent.putExtra(MyInviteActivity.KEY_INVITE_NUM, Integer.parseInt(invitationNum.getText().toString()));
//                    context.startActivity(intent);
                    break;
                case R.id.tv_copy:
                    AllUtils.copyText(getContext(), invitationCodeTv.getText().toString());
                    YMToast.showText("复制成功");
                    break;
                case R.id.ll_my_order:
                    intent = new Intent(context, OrderActivity.class);
                    intent.putExtra("type", 1);
                    startActivity(intent);
                    break;
                case R.id.ll_kehu:
                    intent = new Intent(context, OrderActivity.class);
                    intent.putExtra("type", 2);
                    startActivity(intent);
                    break;
                case R.id.ll_pending:
                    intent = new Intent(context, PendingOrderActivity.class);
                    intent.putExtra(MyRemainActivity.REMAIN_MONEY_KEY, amountTv.getText().toString());
                    startActivity(intent);
                    break;

            }
        }
    }

    @Override
    public void eventMessage(EventMessage eventMessage) {
        super.eventMessage(eventMessage);
        Log.e("xing", "eventMessage");
        //登出后，有区代商城则去除；登录后，如有区代商城则添加；
        if (AllCode.LoginSuccess.equals(eventMessage.getEventType())) {
            if (UserInfoConfig.IsAgent() && mUserFunctionList != null) {
                mUserFunctionList.add(3, new FunctionBean("区代商城", R.drawable.icon_qudai, false));
                mUserFunctionAdapter.notifyDataSetChanged();
            }
            if (UserInfoConfig.getCompanyId() == 3) {
                mUserFunctionList.add(new FunctionBean(StarStore, R.drawable.icon_star, false));
                mUserFunctionAdapter.notifyDataSetChanged();
            }
            if (UserInfoConfig.getRoleId() >= Type.LevelType.VIP_20 && mUserFunctionList != null) {
                mUserFunctionList.add(new FunctionBean("天使计划", R.drawable.icon_angle_1, false));
                mUserFunctionAdapter.notifyDataSetChanged();
            }
            if (youmiDetail != null) {
                youmiDetail.setVisibility(isSpecialAccount() ? View.VISIBLE : View.GONE);
            }

            setVipLayout();
            getOilCardInfo();
        }
        if (Type.EventMsgType.BuyComplete.equals(eventMessage.getEventType()) && Type.EventMsgType.Success == eventMessage.getCode()) {
            if (OilCardView.OilCardGoodsId.equals(eventMessage.getGoodsID())) {
                getOilCardInfo();
            }
        }
        if (AllCode.LoginOut.equals(eventMessage.getEventType())) {
            mPhotoIv.setBackgroundResource(R.drawable.wo_no);
            youmi.setText("0");
            if (mUserFunctionList.get(3).getTitle().equals("区代商城")) {
                mUserFunctionList.remove(3);
                mUserFunctionAdapter.notifyDataSetChanged();
            }
            if (mUserFunctionList.get(mUserFunctionList.size() - 1).equals(StarStore)) {
                mUserFunctionList.remove(mUserFunctionList.size() - 1);
                mUserFunctionAdapter.notifyDataSetChanged();
            }
            if (billItemBeanList != null) {
                if (mUserFunctionList.get(mUserFunctionList.size() - 1).getTitle().equals("天使计划")) {
                    mUserFunctionList.remove(mUserFunctionList.size() - 1);
                    mUserFunctionAdapter.notifyDataSetChanged();
                }
                if (billItemBeanList != null) {
                    billItemBeanList.clear();
                    mBillAdapter.notifyDataSetChanged();
                }
                if (mAngleIv != null) mAngleIv.setVisibility(View.GONE);

            }
        }
    }

    private void getOilCardInfo() {
//        YmApi.getYmService()
//                .oilCardInfo()
//                .compose(bindToLifecycle())
//                .compose(XApi.getApiTransformerData())
//                .compose(XApi.getScheduler())
//                .subscribe(new ApiSubscriber<OilCardData>() {
//                    @Override
//                    public void onSuccess(OilCardData t) {
//                        if (youmi!=null&&t!=null){
//                            youmi.setText(TextUtils.isEmpty(t.getMoney()) ? "0" : t.getMoney());
//                        }
//                    }
//
//                    @Override
//                    public void onFail(ApiException error) {
//                    }
//
//                });
    }

    private void gotoActivity(Class otherActivity) {
        Intent intent = new Intent(context, otherActivity);
        intent.putExtra("agent", mInfo.agent);
        intent.putExtra("roleId", mInfo.roleId);
        context.startActivity(intent);
    }

    @Override
    public void setUserInfo(UserInfo userInfo) {
        super.setUserInfo(userInfo);
        Log.e("xing", "setUserInfo");
        initFData();
        if (mUserFunctionAdapter != null) mUserFunctionAdapter.notifyDataSetChanged();
    }

    public void jumpUrl(WalletUrl data) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra(Constant.DATA, data.url);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     *
     */
    public void setData(MyUserCenterInfo info) {
        Log.e("xing", "setData");
        if (info == null) {
            return;
        }
        mInfo = info;
        copyTv.setVisibility(View.VISIBLE);
//        invitationNum.setText("" + info.getInviteNum());
        totalAmountTv.setText(info.totalMoney);
        mShowYouMi.setText(info.mugwordMoney);
        amountTv.setText(info.money);
        quDaiIv.setVisibility(UserInfoConfig.IsAgent() ? View.VISIBLE : View.GONE);
//        if (info.getUserInfo().getIsAngel() == 1) {
//            mAngleIv.setVisibility(View.VISIBLE);
//        }
//        RequestOptions requestOptions = new RequestOptions();
//        requestOptions.placeholder(R.drawable.goods_default);

        MyUserCenterInfo.UserInfoBean userInfo = info.getUserInfo();
        if (userInfo != null) {
            if (!TextUtils.isEmpty(userInfo.getHeadimgurl())) {
                RequestOptions requestOptions = new RequestOptions();
                requestOptions.placeholder(R.drawable.goods_default);
                Glide.with(this).load(Uri.parse(info.getUserInfo().getHeadimgurl()))
                        .apply(requestOptions)
                        .into(mPhotoIv);
            }
            invitationCodeTv.setText(info.getUserInfo().getInvitation_code());
            if (!TextUtils.isEmpty(userInfo.getNickname())) {
//                if (userInfo.getNickname().length()>6){
//                    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) userNameTv.getLayoutParams();
//                    lp.width = DensityUtil.dp2px(100);
//                    userNameTv.setLayoutParams(lp);
//                }
                userNameTv.setText(userInfo.getNickname());
            }
            userNameTv.requestLayout();
//            if (userInfo.getIsVip() == Constant.USER_CENTER_VIP){
            if (UserInfoConfig.getRoleId() >= Type.LevelType.VIP_7) {
                vipTv.setVisibility(View.VISIBLE);
                topLayout.setBackground(ResUtil.getDrawable(R.drawable.bg_my_top_vip));
//                inviteLayout.setBackground(ResUtil.getDrawable(R.drawable.bg_invite_my_vip));
//                if (UserInfoConfig.getCompanyId()== 1) {
                if (UserInfoConfig.getRoleId() >= Type.LevelType.VIP_50) {
                    vipTv.setText("运营中心");
                } else {
                    vipTv.setText(ResUtil.getString(R.string.my_user_vip));
                }
//                } else {
//                    if (UserInfoConfig.getRoleId() >= LevelType.VIP_50) {
//                        vipTv.setText("商务董事");
//                    }
//                }
            } else {
                vipTv.setVisibility(View.GONE);
                topLayout.setBackground(ResUtil.getDrawable(R.drawable.bg_my_top_normal));
//                inviteLayout.setBackground(ResUtil.getDrawable(R.drawable.bg_invite_my_normal));
            }
        }
//        if (isSpecialAccount()) {
        setVIPData(info);
        initFData();

//        }


    }


    private void setVIPData(MyUserCenterInfo info) {
//        if (UserInfoConfig.getRoleId() == LevelType.VIP_0){
//            if (mBillAdapter != null) {
////                billItemBeanList = new ArrayList<>();
//                mBillAdapter.setNewData(null);
//            }
//            return;
//        }
        if (mBillAdapter == null) {
            setVipLayout();
        }
        myUserCenterInfo = info;
        billItemBeanList = new ArrayList<>();
        billItemBeanList.add(new BillItemBean("云库存明细", "" + info.cloudNum, null));
        billItemBeanList.add(new BillItemBean("实体库存明细", "" + info.entityNum, null));

//        billItemBeanList.add(new BillItemBean("今日订单(¥)", "" + info.getTodayIncome(), null));
//        if ("1".equals(info.getSendMore())) {
//            billItemBeanList.add(new BillItemBean("送多多收益(¥)", "" + info.getSendMoreIncome(), null));
//        }
//        billItemBeanList.add(new BillItemBean("礼包/爆品/优品/课程(¥)", "" + info.getVipAccount(), null));
//        if (UserInfoConfig.getRoleId() >= Type.LevelType.VIP_7) {
//            billItemBeanList.add(new BillItemBean("管理津贴预计(¥)", "" + info.getTeamAccount(), null));
//        }
        if (info.getTaobaoPendingIncome() != null && info.getTaobaoPendingIncome().size() > 1) {
            billItemBeanList.add(new BillItemBean("淘宝结算预计(¥)",
                    info.getTaobaoPendingIncome().get(1), info.getTaobaoPendingIncome()));
        }
        if (info.getTaobaoPendingIncome() != null && info.getTaobaoPendingIncome().size() > 1) {
            billItemBeanList.add(new BillItemBean("京东结算预计(¥)",
                    info.getJdPendingIncome().get(1), info.getJdPendingIncome()));
        }
        if (!TextUtils.isEmpty(info.getCreditCardAccount())) {
            ArrayList<String> data = new ArrayList<>();
            data.add(info.getCreditCardAccount());
            if ("1".equals(info.getYmFinance())) {
                billItemBeanList.add(new BillItemBean("智能用卡结算预计(¥)",
                        info.getCreditCardAccount(), data));
            }
        }
//        if (info.getThisMonthEstimate() != null && info.getThisMonthEstimate().size() > 1) {
//            if (info.getThisMonthEstimate() != null && info.getThisMonthEstimate().size() > 1) {
//                billItemBeanList.add(new BillItemBean(info.getThisMonthEstimate().get(0) + "结算预计(¥)",
//                        info.getThisMonthEstimate().get(1), info.getThisMonthEstimate()));
//            }
//        }
//        if (info.getNextMonthEstimate() != null && info.getNextMonthEstimate().size() > 1) {
//            billItemBeanList.add(new BillItemBean(info.getNextMonthEstimate().get(0) + "结算预计(¥)",
//                    info.getNextMonthEstimate().get(1), info.getNextMonthEstimate()));
//        }
        if (mBillAdapter != null) {
            mBillAdapter.setNewData(billItemBeanList);
            mBillAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this).keyboardEnable(false).init();
    }

}
